<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Http\Controllers;

use App\Exceptions\ApiException;
use App\Format\MessageFormat;
use App\Http\Requests\CreateMessageRequest;
use App\Http\Requests\MessageListRequest;
use App\Services\MessageService;
use Illuminate\Http\Request;

class MessageController extends Controller
{
    protected $messageService;

    public function __construct(MessageService $messageService)
    {
        $this->messageService = $messageService;
    }

    public function getLists(MessageListRequest $messageListRequest)
    {
        $page = $messageListRequest->input('page', 1);
        $pageSize = $messageListRequest->input('page_size', 20);
        $orderColumn = $messageListRequest->input('order_column');
        $orderType = $messageListRequest->input('order_type');
        $search = [];
        $search['user_id'] = $messageListRequest->input('user_id') ?? 0;
        $search['token'] = $messageListRequest->input('token') ?? '';
        if ($messageListRequest->input('title')) {
            $search['title'] = $messageListRequest->input('title');
        }
        if ($messageListRequest->input('start_date')) {
            $search['start_date'] = $messageListRequest->input('start_date');
        }
        if ($messageListRequest->input('end_date')) {
            $search['end_date'] = $messageListRequest->input('end_date');
        }
        $res = $this->messageService->getList($page, $pageSize, $orderColumn, $orderType, $search);
        return renderSuccessJson($res);
    }

    public function getAdminList(Request $request)
    {
        $page = $request->input('page', 1);
        $pageSize = $request->input('page_size', 10);
        $search = [];
        if ($request->input('title')) {
            $search['title'] = $request->input('title');
        }
        if ($request->input('start_date')) {
            $search['start_date'] = $request->input('start_date');
        }
        if ($request->input('end_date')) {
            $search['end_date'] = $request->input('end_date');
        }

        $res = $this->messageService->getAdminList($page, $pageSize, $search);
        return renderSuccessJson($res);
    }

    public function detail(Request $request)
    {
        $id = $request->input('id');
        if (! $id) {
            throw new ApiException('缺少参数');
        }
        $userId = $request->input('user_id');
        $token = $request->input('token');
        $res = $this->messageService->detail($id, $userId, $token);
        return renderSuccessJson($res);
    }

    public function create(CreateMessageRequest $createMessageRequest)
    {
        $format = new MessageFormat($createMessageRequest->all());
        $this->messageService->createMessage($format);
        return renderSuccessJson();
    }

    public function update(Request $request)
    {
        $id = $request->input('id');
        if (empty($id)) {
            throw new ApiException('缺少参数', 1);
        }
        $format = new MessageFormat($request->all());
        $this->messageService->updateMessage($format);
        return renderSuccessJson();
    }

    public function delete(Request $request)
    {
        $id = $request->input('id');
        if (empty($id)) {
            throw new ApiException('缺少参数', 1);
        }
        $this->messageService->delete($id);
        return renderSuccessJson();
    }

    public function checkHaveNotReadMessage(Request $request)
    {
        $userId = $request->input('user_id');
        $res = $this->messageService->checkHaveNotReadMessage($userId);
        return renderSuccessJson($res);
    }
}
